﻿using System;
using System.Windows.Forms;

namespace Constructor
{
    public partial class PropertiesSlot : UserControl
    {
        private Slot _slot;
        public PropertiesSlot()
        {
            InitializeComponent();
        }
        public void Init(Slot slot)
        {
            Visible = true;
            _slot = slot;
            cmb_name.Enabled = !(slot.Frame.Name == "Клетка" || slot.Frame.Name=="Объект");
            cmb_type.Enabled = !(slot.Frame.Name == "Клетка" || slot.Frame.Name == "Объект");
            InitCmbName();
            panel1.Controls.Clear();
            ChangeVisibleAKO();
            cmb_type.SelectedIndexChanged -= cmb_type_SelectedIndexChanged;
            switch (_slot.Type)
            {
                case CommonLib.SlotType.Regular:
                    panel1.Controls.Add(new RegularPart(_slot));
                    cmb_type.SelectedIndex = 0;
                    break;
                case CommonLib.SlotType.AKO:
                    panel1.Controls.Add(new SubframePart(_slot));
                    cmb_type.SelectedIndex = 5;
                    break;
                case CommonLib.SlotType.SubFrame:
                    panel1.Controls.Add(new SubframePart(_slot));
                    cmb_type.SelectedIndex = 2;
                    break;
                case CommonLib.SlotType.Next:
                    panel1.Controls.Add(new SubframePart(_slot));
                    cmb_type.SelectedIndex = 3;
                    break;
                case CommonLib.SlotType.Error:
                    panel1.Controls.Add(new SubframePart(_slot));
                    cmb_type.SelectedIndex = 4;
                    break;
                case CommonLib.SlotType.Production:
                    panel1.Controls.Add(new NewProductionPart(_slot));
                    cmb_type.SelectedIndex = 1;
                    break;
            }            
            panel1.Controls[0].Dock = DockStyle.Fill;
            cmb_type.SelectedIndexChanged += cmb_type_SelectedIndexChanged;
        }
        public void Hide()
        {
            Visible = false;
        }
        public void InitCmbName()
        {
            cmb_name.Items.Clear();
            cmb_name.Items.Add(_slot.Name);
            if (_slot.Frame.ParentFrame != null)
            {
                foreach (Slot slot in _slot.Frame.ParentFrame.AllSlots)
                    cmb_name.Items.Add(slot.Name);
            }
            cmb_name.Text = _slot.Name;
        }
        private void ChangeVisibleAKO()
        {
            if (_slot.Frame.ContainsAKOSlot() && _slot.Type != CommonLib.SlotType.AKO)
            {
                if (cmb_type.Items.Count == 6)
                    cmb_type.Items.RemoveAt(5);
            }
            else
                if (cmb_type.Items.Count == 5)
                    cmb_type.Items.Insert(5, "AKO");
        }
        private void cmb_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_slot != null)
            {
                if (panel1.Controls.Count > 0) panel1.Controls[0].Dispose();
                panel1.Controls.Clear();
                switch (cmb_type.SelectedIndex)
                {
                    case 0:
                        _slot.Type = CommonLib.SlotType.Regular;
                        panel1.Controls.Add(new RegularPart(_slot));
                        break;
                    case 5:
                        _slot.Type = CommonLib.SlotType.AKO;
                        panel1.Controls.Add(new SubframePart(_slot));
                        break;
                    case 2:
                        _slot.Type = CommonLib.SlotType.SubFrame;
                        panel1.Controls.Add(new SubframePart(_slot));
                        break;
                    case 3:
                        _slot.Type = CommonLib.SlotType.Next;
                        panel1.Controls.Add(new SubframePart(_slot));
                        break;
                    case 4:
                        _slot.Type = CommonLib.SlotType.Error;
                        panel1.Controls.Add(new SubframePart(_slot));
                        break;
                    case 1:
                        _slot.Type = CommonLib.SlotType.Production;
                        panel1.Controls.Add(new NewProductionPart(_slot));
                        break;
                }                
                panel1.Controls[0].Dock = DockStyle.Fill;
                _slot.RepaintItem();
                ChangeVisibleAKO();
                InitCmbName();
            }
        }
        private void ChangeName(string name)
        {
            if (_slot == null) return;
            if (name != _slot.Name)
                if (name == "")
                {
                    MessageBox.Show("Имя не может быть пустым");
                    cmb_name.Text = _slot.Name;
                }
                else
                {
                    if (name.Contains("."))
                    {
                        MessageBox.Show("Запрещенный символ \".\"");
                        cmb_name.Text = _slot.Name;
                    }
                    else
                        if (_slot.Frame.SearchSlot(name) == null)
                        {
                            _slot.Name = name;
                            _slot.RepaintItem();
                            _slot.Frame.RepaintNode(true);
                        }
                        else
                        {
                            MessageBox.Show("Слот с таким именем уже существует");
                            cmb_name.Text = _slot.Name;
                        }
                }
        }
        private void cmb_name_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ChangeName(cmb_name.Text.Trim());
            }
        }

        private void cmb_name_Leave(object sender, EventArgs e)
        {
            ChangeName(cmb_name.Text.Trim());
        }

        private void cmb_name_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
